package cn.oracle.eshop.auth.service.impl;

import cn.oracle.eshop.auth.composite.PriorityNode;
import cn.oracle.eshop.auth.dao.AccountPriorityRelationshipDAO;
import cn.oracle.eshop.auth.dao.PriorityDAO;
import cn.oracle.eshop.auth.dao.RolePriorityRelationshipDAO;
import cn.oracle.eshop.auth.domain.PriorityDTO;
import cn.oracle.eshop.auth.domain.PriorityDo;
import cn.oracle.eshop.auth.mapper.PriorityDoMapper;
import cn.oracle.eshop.auth.service.PriorityService;
import cn.oracle.eshop.auth.visitor.PriorityNodeRelateCheckVisitor;
import cn.oracle.eshop.auth.visitor.PriorityNodeRemoveVisitor;
import cn.oracle.eshop.auth.visitor.PriorityNodeVisitor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wyh
 * @ClassName ProrityServiceImpl
 * @date 2022/1/12 /22:27
 */
@Service
@Slf4j
public class PriorityServiceImpl implements PriorityService {
    /***
     *  权限管理模块的DAO组件
     * @author wyh
     * @date 2022/1/14
     * @param null
     * @return
     */
    @Autowired
    private PriorityDAO priopertiyDao;
    /**角色和权限关系管理模块的dao组件
     *
     */
    @Autowired
    private RolePriorityRelationshipDAO rolePriorityRelationshipDAO;
    /**账号和全选关系管理模块的DAO组件*/
    @Autowired
    private AccountPriorityRelationshipDAO accountPriorityRelationshipDAO;
    /****
     *  查询根权限
     * @author wyh
     * @date 2022/1/14
     * @param
     * @return 根权限集合
     */
    @Override
    public List<PriorityDTO> listRootPriorites() {
        List<PriorityDo> priorityDos = priopertiyDao.listRootPriorites();
        List<PriorityDTO> priorityDtoList = new ArrayList<>();
        for (PriorityDo priorityDo : priorityDos) {
            priorityDtoList.add(priorityDo.clone(PriorityDTO.class));
        }
        return priorityDtoList;
    }

    /***
     * 根据父权限ID查询子权限
     * @author wyh
     * @date 2022/1/22
     * @param parentId
     * @return java.util.List<cn.oracle.eshop.auth.domain.PriorityDTO>
     */
    @Override
    public List<PriorityDTO> listChildPriorities(long parentId) {
        List<PriorityDTO> priorityDtoList = null;
        try {
            List<PriorityDo> priorityDos = priopertiyDao.listChildPriorities(parentId);
            priorityDtoList = new ArrayList<>();
            if (priorityDos == null) {
                return null;
            }
            for (PriorityDo priorityDo : priorityDos) {
                priorityDtoList.add(priorityDo.clone(PriorityDTO.class));
            }
        } catch (Exception e) {
            log.error("getChildError:{}", e);
        }
        return priorityDtoList;
    }

    @Override
    public Boolean savePriority(PriorityDTO priorityDTO) {
        try {

            PriorityDo priorityDo = priorityDTO.clone(PriorityDo.class);
            priopertiyDao.savePriority(priorityDo);
        } catch (Exception e) {

            log.error("saveError：{}", e);
            return false;
        }

        return true;
    }

    @Override
    public Boolean updatePriorityDo(PriorityDTO priorityDTO) {

        try {
            priopertiyDao.updatePriorityDo(priorityDTO.clone(PriorityDo.class));
        } catch (Exception e) {

            log.error("saveError：{}", e);
            return false;
        }

        return true;
    }

    @Override
    public PriorityDTO getPriorityById(long id) {
        List<PriorityDTO> priorityDtoList = null;
        try {
            PriorityDo priorityDo = priopertiyDao.getPriorityById(id);

            if (priorityDo == null) {
                return null;
            }
            return priorityDo.clone(PriorityDTO.class);

        } catch (Exception e) {
            log.error("getChildError:{}", e);
        }
        return null;
    }
/**删除权限*/
    @Override
    public Boolean removePriority(Long id) {
        try {
            //根据ID查询权限
            PriorityDo priorityDo = priopertiyDao.getPriorityById(id);
            PriorityNode priorityNode = priorityDo.clone(PriorityNode.class);
            //检查这个权限是否以及其下任何一个子权限，是否被角色或者账号给关联着
            PriorityNodeRelateCheckVisitor relateCheckVisitor=new PriorityNodeRelateCheckVisitor(priopertiyDao,rolePriorityRelationshipDAO,accountPriorityRelationshipDAO);
            relateCheckVisitor.visit(priorityNode);
            Boolean resultCheackResult= relateCheckVisitor.getRelateCheckResult();
            if(resultCheackResult){
                return false;
            }
            //递归删除当前权限以及其下所有的子权限。
            PriorityNodeRemoveVisitor removeVisitor=new PriorityNodeRemoveVisitor(priopertiyDao);
            removeVisitor.visit(priorityNode);
        }catch (Exception e){
            return false;
        }
        return true;
    }
}
